home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / mail / mailleds.93 / mailleds / mailleds-0.93 / mailfile.c < prev    next >
C/C++ Source or Header  |  1996-05-14  |  2KB  |  85 lines

  1.  
  2.  
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <sys/types.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include "config.h"
  14. #include "mailleds.h"
  15.  
  16.  
  17. extern int uid;
  18. extern sig_atomic_t exit_now;
  19. extern char *username;
  20. extern char opt_u;
  21.  
  22. char *
  23.  get_mailfile()
  24. {
  25.     char *mailfile;
  26.     if (opt_u || (mailfile = getenv("MAIL")) == (char *) NULL) {
  27.         mailfile = xmalloc((strlen(MAILDIR) + 1 + strlen(username) + 1) * sizeof(char));
  28.         sprintf(mailfile, "%s/%s", MAILDIR, username);
  29.     }
  30.     return (mailfile);
  31. }
  32.  
  33. int count_occurences(file, exp)
  34. FILE *file;
  35. char *exp;
  36. {
  37.     char buf[1024];
  38.     int len, matches;
  39.  
  40.     len = strlen(exp);
  41.     matches = 0;
  42.  
  43.     while (fgets(buf, 1024, file) != NULL) {
  44.         if (strncmp(buf, exp, len) == 0)
  45.             matches++;
  46.     }
  47.  
  48.     rewind(file);
  49.     return (matches);
  50. }
  51.  
  52. /* Subtract messages from messages w/ a "status:" line to get new messages */
  53. int count_mail(mailbox)
  54. FILE *mailbox;
  55. {
  56.     int From, Status;
  57.     From = count_occurences(mailbox, "From ");
  58.     Status = count_occurences(mailbox, "Status: R");
  59.     return (From - Status);
  60. }
  61.  
  62. /* Test and open the mailbox file -- hang on the mailbox if -f */
  63. FILE *
  64.  open_mailbox_file(file)
  65. char *file;
  66. {
  67.     FILE *mailbox;
  68.     char *errmsg;
  69.     if ((mailbox = fopen(file, "r")) == (FILE *) NULL) {
  70.         if (errno == ENOENT) {
  71.             while ((mailbox = fopen(file, "r")) == NULL) {
  72.                 if (exit_now)
  73.                     exit_cleanly();
  74.                 sleep(INTERVAL_BETWEEN_CHECKS);
  75.             }
  76.         } else {
  77.             errmsg = xmalloc((strlen("mailleds: ") + strlen(file) + 1) * sizeof(char));
  78.             sprintf(errmsg, "mailleds: %s", file);
  79.             perror(errmsg);
  80.             exit_fatal("Cannot read mailbox file");
  81.         }
  82.     }
  83.     return (mailbox);
  84. }
  85.